package com.example.gokuplayalong.service.fragments.others;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.donkingliang.labels.LabelsView;
import com.example.gokuplayalong.MainActivity;
import com.example.gokuplayalong.R;
import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.beans.Beans;
import com.example.gokuplayalong.bases.custom.UserBaseViewHolder;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.tools.Logger;
import com.example.gokuplayalong.bases.tools.Tools;
import com.example.gokuplayalong.service.fragments.users.receive.UserMainReceiveUserInfo;
import com.example.gokuplayalong.service.fragmentsmanage.SysManagerFragments;
import com.kongzue.dialogx.dialogs.PopTip;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class UserMainPageReceiveViewHolder extends UserBaseViewHolder {


    Beans bean;
    private ImageView mUserMainReceiveItemHeadshot;
    private TextView mUserMainReceiveItemMerchantName;
    private TextView mUserMainReceiveItemMerchantDesc;
    private TextView mUserMainReceiveItemMerchantScore;
    private TextView mUserMainReceiveItemGuide;
    private LabelsView mUserMainReceiveItemLabels;
    private TextView mUserMainReceiveItemFind;

    public UserMainPageReceiveViewHolder(@NonNull View itemView) {
        super(itemView);
        mUserMainReceiveItemHeadshot = itemView.findViewById(R.id.user_main_receive_item_headshot);
        mUserMainReceiveItemMerchantName = itemView.findViewById(R.id.user_main_receive_item_merchant_name);
        mUserMainReceiveItemMerchantDesc = itemView.findViewById(R.id.user_main_receive_item_merchant_desc);
        mUserMainReceiveItemMerchantScore = itemView.findViewById(R.id.user_main_receive_item_merchant_score);
        mUserMainReceiveItemGuide = itemView.findViewById(R.id.user_main_receive_item_guide);
        mUserMainReceiveItemLabels = itemView.findViewById(R.id.user_main_receive_item_labels);
        mUserMainReceiveItemFind = itemView.findViewById(R.id.user_main_receive_item_find);

        mUserMainReceiveItemFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    Message message = new Message();
                    message.what = 1;
                    Bundle bundle = new Bundle();
                    bean.getJsonObject().put("position", getAbsoluteAdapterPosition());
                    bundle.putString("bean", bean.getJsonObject().toJSONString());
                    message.setData(bundle);

                    UserMainReceiveUserInfo userMainReceiveUserInfo = (UserMainReceiveUserInfo) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("UserMainReceiveUserInfo");
                    if (userMainReceiveUserInfo != null) {
                        userMainReceiveUserInfo.handler.sendMessage(message);
                        userMainReceiveUserInfo.handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    SysManagerFragments.switchOnSysFragment("UserMainReceiveUserInfo");
                                } catch (Throwable e) {

                                }
                            }
                        }, 200);
                    }

                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });


    }

    @Override
    public void bindView(List list, Object bean) {
        try {
            if (bean instanceof Beans) {
                this.bean = (Beans) bean;
                initView();

            }
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    private void initView() {
        try {

            String filename = bean.getJsonObject()
                    .getString("phone") + "_merchant_headshot_upload.png";

            Picasso.with(MainActivity.getInstance())
                    .load(AppParams.getInstance().uploadMediaLocationServer + "/" + filename)
                    //.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .into(mUserMainReceiveItemHeadshot);


            JSONObject data = JSON.parseObject(JSON.parseObject(bean.getJsonObject()
                    .getString("merchantInfo")).getString("data"));
            if (data != null) {
                if (!StringUtils.isEmpty(data.getDoubleValue("merchantScore") + "")) {
                    mUserMainReceiveItemMerchantScore.setText(String.format("%s分", data.getDoubleValue("merchantScore")));
                }
                else {
                    mUserMainReceiveItemMerchantScore.setText("0.0分");
                }

                if (!StringUtils.isEmpty(data.getString("merchantName"))) {
                    mUserMainReceiveItemMerchantName.setText(data.getString("merchantName"));
                }
                else {
                    if (Objects.equals(data.getString("bodyType"), "企业") || Objects.equals(data.getString("bodyType"), "个体工商户")) {
                        mUserMainReceiveItemMerchantName.setText(data.getString("companiesMerchantAbbreviation"));
                    }
                    else if (Objects.equals(data.getString("bodyType"), "个人")) {
                        mUserMainReceiveItemMerchantName.setText(data.getString("individualMerchantAbbreviation"));
                    }
                }

                if (!StringUtils.isEmpty(data.getString("merchantManifesto"))) {
                    mUserMainReceiveItemMerchantDesc.setText(data.getString("merchantManifesto"));
                }
                JSONArray skillsList = JSON.parseArray(bean.getJsonObject()
                        .getString("skillsList"));

                ArrayList<String> label = new ArrayList<>();


                for (int i = 0; i < skillsList.size(); i++) {
                    label.add(skillsList.getJSONObject(i).getString("gametype"));
                }
                mUserMainReceiveItemLabels.setMaxColumns(3);
                mUserMainReceiveItemLabels.setMaxLines(1);
                mUserMainReceiveItemLabels.setLabels(label); //直接设置一个字符串数组就可以了

            }
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

}
